package org.bing.zion.helper;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.SocketAddress;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class ChannelBuilder {

	public static ServerSocketChannel newServerSocketChannel(String host, int port) throws IOException {
		ServerSocketChannel sschannel = ServerSocketChannel.open();
		sschannel.configureBlocking(false);
		ServerSocket ss = sschannel.socket();
		SocketAddress address = host == null ? new InetSocketAddress(port) : new InetSocketAddress(host, port);
		ss.bind(address);
		return sschannel;
	}

	public static SocketChannel newSocketChannel(String host, int port) throws IOException {
		SocketAddress addr = new InetSocketAddress(host, port);
		SocketChannel channel = SocketChannel.open(addr);
		channel.configureBlocking(false);
		return channel;
	}
}
